package com.hourse.platform.service.impl;

import com.hourse.platform.service.AuthService;
import com.hourse.platform.utils.AppTools;
import com.hourse.platform.utils.SqlParams;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AuthServiceImpl extends PublicServiceImpl implements AuthService {


  @Transactional(readOnly = true)
  @Override
  public Map<String, List<Object>> getUserPageAuthInfo() {

      SqlParams sqlParams = null;
      sqlParams = SqlParams.New().append(" select att.mid,att.mbid,e.attr ,e.url  from (")
              .append(" select c.menu_id as mid,c.menu_button_id as mbid from role_menu_button  c where c.role_id in(")
              .append(" select DISTINCT w.role_id from user_role b ")
              .append(" left join  role_group_item w on b.role_id = w.role_group_id")
              .append("  where b.user_id = :userId")
              .append(" ) ) as att left join menu_button e on  e.id =  mbid ")
              .add("userId", AppTools.GetCurrentUserId());

    Map<String, List<Object>> authInfoMap = new HashMap<>();

    List<Object[]> list = baseDao.getItemListByNativeSQL(sqlParams.getSql(),sqlParams.getParams());

    for(Object[] obj : list){

      String menuId = String.valueOf(obj[0]);
      String menuAttr = String.valueOf(obj[2]);

      if(authInfoMap.containsKey(menuId)){
        authInfoMap.get(menuId).add(menuAttr);
      } else {
        List<Object> authInfo = new ArrayList<>();
        authInfo.add(menuAttr);
        authInfoMap.put(menuId,authInfo);
      }
    }
    return authInfoMap;
  }
}
